package com.kitty.game.jiutian;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.achieve.body.BossImgType;
import com.kitty.game.activity.model.product.ActivityType;
import com.kitty.game.activity.model.product.SuperBoss;
import com.kitty.game.activity.service.boos.service.BoosService;
import com.kitty.game.activity.service.time.FightActivityHandler;
import com.kitty.game.boss.config.BossSet;
import com.kitty.game.boss.model.BossFightParam;
import com.kitty.game.boss.model.BossParam;
import com.kitty.game.boss.model.SuperBossParam;
import com.kitty.game.boss.model.SuperBossZPParam;
import com.kitty.game.boss.service.NewBossService;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.common.BoosTitleDeal;
import com.kitty.game.config.NPC;
import com.kitty.game.enter.TitleInfo;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fight.ai.model.RoundSkillUnit;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.service.FightService;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.i18n.I18nIdDataPool;
import com.kitty.game.map.service.MapService;
import com.kitty.game.npc.service.NewNpcService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.game.utils.Const;
import com.kitty.listener.event.FightEndEvent;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;

import com.kitty.web.common.StringUtils;
import org.apache.mina.core.session.IoSession;
import org.nutz.lang.util.NutMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@Controller
public class JiuTianController extends FightActivityHandler {
    Logger logger = LoggerFactory.getLogger(JiuTianController.class);
    @Autowired
    EquipService equipService;
    @Autowired
    FightService fightService;
    @Autowired
    ChatService chatService;
    private static final List<String> SUPER_BOSS_LIST = Arrays.asList("昊天君","阳天君","赤天君","朱天君", "成天君", "幽天君", "玄天君", "变天君","均天君");


    private static final List<Byte> POLARS = Arrays.asList(Const.SCHOOL_METAL, Const.SCHOOL_WOOD, Const.SCHOOL_WATER, Const.SCHOOL_FIRE, Const.SCHOOL_EARTH);

    @Autowired
    protected TeamService teamService;
    @Autowired
    protected NewBossService bossService;
    @Autowired
    protected NewNpcService npcService;
    @Autowired
    private MapService mapService;


    private Map<String, SuperBoss> superBossMap = new HashMap<>();
    
    private static byte index = 0;

    /**
     * 最少组队人数
     */
    private static final int MIN_TEAM_COUNT = 1;
    private static final String TEAM_COUNT_NOT_ENOUGH = "你还是凑齐三个人再来向我挑战吧!";
    private static final int ACCEPT_MIN_LEVEL = 40;
    private static final String TEAM_LEVEL_NOT_ENOUGH = "#Y{0}#n的等级还没有{1}级。";
    /**
     * 最大战胜次数
     */
    private static final short MAX_WIN_COUNT = 100;
    /**
     * 队员之间最大等级差
     */
    private static final int MAX_LEVEL_DIFF = 100;

    private static final Map<Byte, Integer> POLAR_SKILLS = new HashMap<>();

    private static final Map<Byte, Integer> POLAR_HELP_SKILLS = new HashMap<>();
    @PostConstruct
    private void init() {
        SuperBoss superBoss = new SuperBoss("昊天君", 6410, 701);
        superBoss.getArtifactMap().put("混元金斗", 18);
        superBoss.getArtifactMap().put("番天印", 18);
        superBoss.getArtifactMap().put("卸甲金葫", 18);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("阳天君", 6411, 702);
        superBoss.getArtifactMap().put("混元金斗", 19);
        superBoss.getArtifactMap().put("番天印", 19);
        superBoss.getArtifactMap().put("卸甲金葫", 19);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("赤天君", 6412, 703);
        superBoss.getArtifactMap().put("混元金斗", 20);
        superBoss.getArtifactMap().put("番天印", 20);
        superBoss.getArtifactMap().put("卸甲金葫", 20);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("朱天君", 6413, 704);
        superBoss.getArtifactMap().put("混元金斗", 21);
        superBoss.getArtifactMap().put("番天印", 21);
        superBoss.getArtifactMap().put("卸甲金葫", 21);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("成天君", 6414, 702);
        superBoss.getArtifactMap().put("混元金斗", 22);
        superBoss.getArtifactMap().put("番天印", 22);
        superBoss.getArtifactMap().put("卸甲金葫", 22);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("幽天君", 6415, 702);
        superBoss.getArtifactMap().put("番天印", 23);
        superBoss.getArtifactMap().put("混元金斗", 23);
        superBoss.getArtifactMap().put("番天印", 23);
        superBoss.getArtifactMap().put("定海珠", 6);
        superBoss.getArtifactMap().put("卸甲金葫", 23);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("玄天君", 6416, 702);
        superBoss.getArtifactMap().put("番天印", 24);
        superBoss.getArtifactMap().put("混元金斗", 24);
        superBoss.getArtifactMap().put("番天印", 24);
        superBoss.getArtifactMap().put("定海珠", 6);
        superBoss.getArtifactMap().put("卸甲金葫", 24);
        superBoss.getArtifactMap().put("阴阳镜", 5);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("变天君", 6417, 702);
        superBoss.getArtifactMap().put("番天印", 24);
        superBoss.getArtifactMap().put("混元金斗", 24);
        superBoss.getArtifactMap().put("番天印", 24);
        superBoss.getArtifactMap().put("定海珠", 6);
        superBoss.getArtifactMap().put("卸甲金葫", 24);
        superBoss.getArtifactMap().put("阴阳镜", 5);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        superBoss = new SuperBoss("钧天君", 6418, 702);
        superBoss.getArtifactMap().put("番天印", 24);
        superBoss.getArtifactMap().put("混元金斗", 24);
        superBoss.getArtifactMap().put("番天印", 24);
        superBoss.getArtifactMap().put("定海珠", 6);
        superBoss.getArtifactMap().put("卸甲金葫", 24);
        superBoss.getArtifactMap().put("阴阳镜", 5);
        superBoss.setSmallPolarRandom(true);
        superBoss.setSmallSkillRandom(true);
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_METAL,Arrays.asList(12,13,15,31,32,33,35,25,23,22) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WOOD,Arrays.asList(62,63,65,81,82,83,85,75,75,75,75) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_WATER,Arrays.asList(111,112,113,131,132,133,135) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_FIRE,Arrays.asList(162,163,165,181,182,183,185,175,173,172) );
        superBoss.getPolarSmallSkills().put(Const.SCHOOL_EARTH,Arrays.asList(211,212,213,231,232,233,235,225,223,222) );
        superBossMap.put(superBoss.getName(), superBoss);

        /**各门派的5阶攻击技能*/
        POLAR_SKILLS.put(Const.SCHOOL_METAL, 15);
        POLAR_SKILLS.put(Const.SCHOOL_WOOD, 65);
        POLAR_SKILLS.put(Const.SCHOOL_WATER, 114);
        POLAR_SKILLS.put(Const.SCHOOL_FIRE, 165);
        POLAR_SKILLS.put(Const.SCHOOL_EARTH, 214);

        /**各门派的5阶障碍技能*/
        POLAR_HELP_SKILLS.put(Const.SCHOOL_METAL, 25);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_WOOD, 75);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_WATER, 125);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_FIRE, 175);
        POLAR_HELP_SKILLS.put(Const.SCHOOL_EARTH, 225);
    }

    @RequestMapping
    public String JIUTIANZHENJUN_ITEM(IoSession session, JiuTianRespMasterInfo jiuTianRespMasterInfo) {
        Role role = SessionUtils.getRoleBySession(session);
        NPC npc = new NPC();
        if(jiuTianRespMasterInfo.getIndex()==0){
            npc.setIcon(6410);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("昊天君");
            npc.setBossSetName("昊天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==1){
            npc.setIcon(6411);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("阳天君");
            npc.setBossSetName("阳天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==2){
            npc.setIcon(6412);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("赤天君");
            npc.setBossSetName("赤天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==3){
            npc.setIcon(6413);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("朱天君");
            npc.setBossSetName("朱天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==4){
            npc.setIcon(6414);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("成天君");
            npc.setBossSetName("成天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==5){
            npc.setIcon(6415);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("幽天君");
            npc.setBossSetName("幽天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==6){
            npc.setIcon(6416);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("玄天君");
            npc.setBossSetName("玄天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==7){
            npc.setIcon(6417);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("变天君");
            npc.setBossSetName("变天君");
            bossService.addTaskNpc(npc);
        }else if(jiuTianRespMasterInfo.getIndex()==8){
            npc.setIcon(6418);
            npc.setId(SpringUtils.getBean(NewBossService.class).getTempNpcId());
            npc.setType(NPC.TYPE_JIU_TIAN);
            npc.setName("钧天君");
            npc.setBossSetName("钧天君");
            bossService.addTaskNpc(npc);
        }
        String jiance = getNpcContentNotFight(role,npc);  //检测通过才下执行
        if(jiance!=null){
            MessagePusher.pushMessage(role, new RespMsg(jiance));
            return null;
        }
        index = jiuTianRespMasterInfo.getIndex();
        doStartFight(role,npc);
        return null;
    }

    private byte getRoleRemainCount(Role role) {
        return (byte) SpringUtils.getActivityService().getRemainCount(role, ActivityType.JTZJ);
    }

    @Override
    protected String getNpcContent(Role role, NPC bossNpc) {
        return null;
    }

    @Override
    protected String getNpcContentNotFight(Role role, NPC bossNpc) {
        /**检测队伍人数*/
        int teamCount = teamService.getTeamCount(role);
        if (teamCount < MIN_TEAM_COUNT) {
            return TEAM_COUNT_NOT_ENOUGH;
        }

        /**检测等级*/
        Team team = teamService.getTeam(role.getRoleId());
        String names = teamService.checkMember(team, memberRole -> memberRole.getLevel() < ACCEPT_MIN_LEVEL);
        if (names != null) {
            return MessageFormat.format(TEAM_LEVEL_NOT_ENOUGH, names, ACCEPT_MIN_LEVEL);
        }

        /**
         * 检测挑战次数
         */
        names = teamService.checkMember(team, memberRole -> getRoleRemainCount(memberRole) <= 0);
        if (names != null) {
            return "#R" + names + "#n没有挑战次数";
        }
        if (getRoleRemainCount(role) <= 0) {
            return "#R" + role.getName() + "#n没有挑战次数";
        }

        /**
         * 队员之间最大等级差
         */
        names = teamService.checkMember(team, memberRole -> teamService.checkMember(team, mRole -> memberRole.getLevel() - mRole.getLevel() > MAX_LEVEL_DIFF) != null);
        if (names != null) {
            return I18nIdDataPool.getI18nContent(I18nId.PMT_1017, MAX_LEVEL_DIFF);
        }
        return null;
    }

    @Override
    protected void doStartFight(Role role, NPC bossNpc) {
        SuperBoss superBoss = getSuperBoss(bossNpc.getName());
        superBoss.setFightCount(superBoss.getFightCount()+1);
        List<BossParam> bossParamList = newBossParamList(role, bossNpc);
        BossFightParam bossFightParam = new BossFightParam(bossParamList, getFightType(role));
        bossFightParam.setNpcId(bossNpc.getId());
        int maxRoleLevel = teamService.getMaxRoleLevelInTeam(role);
        bossFightParam.setNpcLevel((short) maxRoleLevel);
        Fight fight = bossService.startFightToBoss(role, bossFightParam);
        fight.setMaxRound(150);
        if (superBoss.getMainShowAfterRound() <= 0) {
            addShouting(fight);
        }

    }

    private void addShouting(Fight fight) {
        if (fight.getData() == null) {
            fight.setData(NutMap.NEW());
        }
        String content = I18nIdDataPool.getI18nContent(I18nId.PMT_1008);
        fight.getData().setv("fight_round_start_shouting", Arrays.asList(content));
        fight.getData().setv("fight_round_start_shouting_period", 5);
    }

    @Override
    protected int getFightType(Role role) {
        return Const.jiutian;
    }

    @Override
    protected NPC getBossNpc(int npcId) {
        return SpringUtils.getMapService().getNpc(npcId);
    }

    @Override
    protected void clearNpcAfterWin(Role role, NPC bossNpc) {

    }

    private SuperBoss getSuperBoss(String name) {
        return superBossMap.get(name);
    }

    private List<BossParam> newBossParamList(Role role, NPC bossNpc) {
        SuperBoss superBoss = getSuperBoss(bossNpc.getName());
        List<BossParam> bossParamList = new ArrayList<>();
        int count = 10;
        SuperBossParam superBossParam = newSuperBossParam(bossNpc);
        superBossParam.setMainShowAfterRound(superBoss.getMainShowAfterRound());
        superBossParam.setSmallEscapeAfterRound(superBoss.getSmallEscapeAfterRound());
        if (superBoss.isSmallPolarRandom()) {
            byte polar = getRandomPolar();
            superBossParam.setPolar(polar);
        }
        if (superBoss.isSmallSkillRandom()) {
            ArrayList<Integer> skillIds = new ArrayList<>(superBoss.getPolarSmallSkills().get((byte)superBossParam.getPolar()));
            superBossParam.setSkillIds(skillIds);
        }
        bossParamList.add(superBossParam);
        String boy_name[] = new String[1];
        if(index==0){
            boy_name = new String[]{"一阶天兵1", "一阶天兵2"};
        }else if(index==1){
            boy_name = new String[]{"二阶天兵1","二阶天兵2"};
        }else if(index==2){
            boy_name = new String[]{"三阶天兵1","三阶天兵2"};
        }else if(index==3){
            boy_name = new String[]{"四阶天兵1","四阶天兵2"};
        }else if(index==4){
            boy_name = new String[]{"五阶天兵1","五阶天兵2"};
        }else if(index==5){
            boy_name = new String[]{"六阶天兵1","六阶天兵2"};
        }else if(index==6){
            boy_name = new String[]{"七阶天兵1","七阶天兵2"};
        }else if(index==7){
            boy_name = new String[]{"八阶天兵1","八阶天兵2"};
        }else if(index==8){
            boy_name = new String[]{"九阶天兵1","九阶天兵2"};
        }
        /**加count-1个*/
        int smallCount = superBoss.getZPName() != null ? count - 1 : count;
        for (int i = 1; i < smallCount; i++) {
            int index = (int) (Math.random() * boy_name.length);
            BossSet bossSet = bossService.getBossSet(boy_name[index]);
            BossParam bossParam = new BossParam(bossSet, bossSet.getName());
            if (superBoss.isSmallPolarRandom()) {
                byte polar = SpringUtils.getBean(BoosService.class).getRandomPolar();
                bossParam.setPolar(polar);
            }
            if (superBoss.isSmallSkillRandom()) {
                ArrayList<Integer> skillIds = new ArrayList<>(superBoss.getPolarSmallSkills().get((byte) bossParam.getPolar()));
                bossParam.setSkillIds(skillIds);
            }
            bossParamList.add(bossParam);
        }
        if (superBoss.getZPName() != null) {
            BossSet bossSet = bossService.getBossSet(superBoss.getZPName());
            bossParamList.add(new SuperBossZPParam(bossSet, bossSet.getName()));
        }

        return bossParamList;
    }

    /**获得随机相性*/
    private byte getRandomPolar() {
        int index = ThreadLocalRandom.current().nextInt(POLARS.size());
        return POLARS.get(index);
    }


    private SuperBossParam newSuperBossParam(NPC bossNpc) {
        SuperBoss superBoss = getSuperBoss(bossNpc.getName());
        BossSet bossSet = bossService.getBossSet(bossNpc.getName());
        SuperBossParam bossParam = new SuperBossParam(bossSet, bossNpc.getName());
        /**随机相性*/
        byte polar = getRandomPolar();
        bossParam.setPolar(polar);
        /**获得对应的技能*/
        ArrayList<Integer> skillIds = getSkillIdsMatchPolar(polar);
        bossParam.setSkillIds(skillIds);
        bossParam.setRoundSkills(getRoundSkills(polar, superBoss));
        /**对应的法宝*/
        bossParam.setArtifactMap(superBoss.getArtifactMap());
        return bossParam;
    }

    public List<RoundSkillUnit> getRoundSkills(byte polar, SuperBoss superBoss) {
        List<RoundSkillUnit> list = new ArrayList<>();
        /**妖皇天怒*/
        list.add(new RoundSkillUnit(5, 5, superBoss.getSkillId()));
        Integer skillId = POLAR_HELP_SKILLS.get(polar);
        list.add(new RoundSkillUnit(6, 5, skillId));
        return list;
    }

    /**获得匹配相性的技能ID列表*/
    private ArrayList<Integer> getSkillIdsMatchPolar(byte polar) {
        ArrayList<Integer> skilIds = new ArrayList<>();
        /**相性的技能*/
        //Integer skillId = POLAR_SKILLS.get(polar);
        //skilIds.add(skillId);

        /**力破千钧*/
        skilIds.add(501);
        /**普通攻击*/
        skilIds.add(2);

        return skilIds;
    }


    @Override
    protected void giveReward(Role role, FightEndEvent fightEndEvent, NPC bossNpc) {
        MSGJiuTianRespMasterInfo msgJiuTianRespMasterInfo = JSONObject.parseObject(role.getJuntianJson(), MSGJiuTianRespMasterInfo.class);
        //MSGJiuTianRespMasterInfo msgJiuTianRespMasterInfo = (MSGJiuTianRespMasterInfo) JSONObject.toBean(JSONObject.fromObject(role.getJuntianJson()), MSGJiuTianRespMasterInfo.class);
        if(msgJiuTianRespMasterInfo==null){
            MSGJiuTianRespMasterInfo Info = role.getMsgjuntian();
            //比如当前挑战从0开始  挑战第一关   但是队友已经打到了第二关 1   那么队友不应该给奖励  只有当前关卡的人员有奖励
            if(index==Info.getCurCheckpoint()){
                if (!teamService.isInTeam(role)) {
                    MessagePusher.pushMessage(role, new RespNotifyMiscEx("离队无法获得奖励"));
                    return;
                }
                // 这里改成公共获取配置 经验，道行，武学 奖励。 不再单独写
                fightService.sendBossBasicsReward(role,fightEndEvent);
                //获取道具奖励
                fightService.sendBossExtReward(role,fightEndEvent);

                MSGJiuTianRespMasterInfo msg = new MSGJiuTianRespMasterInfo();
                msg.setCurCheckpoint((byte) (index+1));
                MSGJiuTianRespMasterInfo info = role.getMsgjuntian();
                msg.setOpenMax((byte) (info.getOpenMax()+1));
                msg.setIs_open((byte)1);
                msg.setCount((byte) (info.getOpenMax()+1));
                msg.setIndex((byte) (index+1));
                role.setMsgjuntian(msg);
                MessagePusher.pushMessage(role, msg);
                role.setJuntianJson(JSON.toJSONString(msg));
                role.save();
                logger.error("角色对象上保存九天真君数据=={}=={}",role.getRoleId(),role.getName());
            }else{
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("当前关卡已经挑战过，无法再次获得奖励"));
                return;
            }
        }else{
            //比如当前挑战从0开始  挑战第一关   但是队友已经打到了第二关 1   那么队友不应该给奖励  只有当前关卡的人员有奖励
            if(index==msgJiuTianRespMasterInfo.getCurCheckpoint()){
                if (!teamService.isInTeam(role)) {
                    MessagePusher.pushMessage(role, new RespNotifyMiscEx("离队无法获得奖励"));
                    return;
                }
                // 这里改成公共获取配置 经验，道行，武学 奖励。 不再单独写
                fightService.sendBossBasicsReward(role,fightEndEvent);
                //获取道具奖励
                fightService.sendBossExtReward(role,fightEndEvent);


                MSGJiuTianRespMasterInfo msg = new MSGJiuTianRespMasterInfo();
                msg.setCurCheckpoint((byte) (index+1));
                MSGJiuTianRespMasterInfo info = role.getMsgjuntian();
                msg.setOpenMax((byte) (info.getOpenMax()+1));
                msg.setIs_open((byte)1);
                msg.setCount((byte) (info.getOpenMax()+1));
                msg.setIndex((byte) (index+1));
                role.setMsgjuntian(msg);
                MessagePusher.pushMessage(role, msg);
                role.setJuntianJson(JSON.toJSONString(msg));
                role.save();
                logger.error("角色对象上保存九天真君数据=={}=={}",role.getRoleId(),role.getName());
            }else{
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("当前关卡已经挑战过，无法再次获得奖励"));
                return;
            }
        }
    }

    @Override
    protected void clearNpcTimeOut(NPC npc) {
        timeOut(npc);
    }

    private void timeOut(NPC npc) {
        /**已经消失*/
        if (mapService.getNpc(npc.getId()) == null) {return ;}

        clearSuperBoss(npc);
    }

    private void clearSuperBoss(NPC npc) {
        if (npc == null) {
            return;
        }

        SuperBoss superBoss = getSuperBoss(npc.getName());
        superBoss.setMapId(0);
        bossService.delTaskNpc(npc);
        bossService.broadcastNpcHide(null, npc);
        removeUsedPosition(npc.getMapId(), npc.getX(), npc.getY());
    }

    @Override
    public void doFightFail(Role role, FightEndEvent fightEndEvent) {
        super.doFightFail(role, fightEndEvent);
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        if (npc != null){
            SuperBoss superBoss = getSuperBoss(npc.getName());
            superBoss.setFightCount(superBoss.getFightCount() - 1);
        }
        teamService.memberHandleThreadLocal(role, memberRole -> SpringUtils.getRoleService().punishFightDead(memberRole));
    }


    @Override
    public void doFightWin(Role role, FightEndEvent fightEndEvent) {
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        if (npc != null){
            SuperBoss superBoss = getSuperBoss(npc.getName());
            superBoss.setWinCount(superBoss.getWinCount() + 1);
            /**胜利次数达到最大值*/
            if (superBoss.getWinCount() >= MAX_WIN_COUNT) {
                if (npc != null) {
                    clearSuperBoss(npc);
                }
            }
        }
        super.doFightWin(role, fightEndEvent);
        teamService.memberHandleThreadLocal(role, memberRole -> SpringUtils.getActivityService().addFinishCount(memberRole, ActivityType.JTZJ, 1));
    }
    //胜利Boss次数

    @Override
    public void doFightRunAway(Role role, FightEndEvent fightEndEvent) {
        super.doFightRunAway(role, fightEndEvent);
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        if (npc != null){
            SuperBoss superBoss = getSuperBoss(npc.getName());
            superBoss.setFightCount(superBoss.getFightCount() - 1);
        }

        teamService.memberHandleThreadLocal(role, memberRole -> SpringUtils.getRoleService().punishFightDead(memberRole));
    }

    @Override
    public void doFightMaxRound(Role role, FightEndEvent fightEndEvent) {
        super.doFightMaxRound(role, fightEndEvent);
        NPC npc = mapService.getNpc(fightEndEvent.getNpcId());
        SuperBoss superBoss = getSuperBoss(npc.getName());
        superBoss.setFightCount(superBoss.getFightCount() - 1);
    }

    @Override
    public boolean isSetInFight() {
        /**不需要设置进入战斗状态，可以同时进入战斗*/
        return false;
    }

    @Override
    public void doFightWinForTeam(Team team, FightEndEvent fightEndEvent, NPC bossNpc) {
        if(SUPER_BOSS_LIST.contains(bossNpc.getName())){
            String titleName = getTitleName(bossNpc.getName());
            if(StringUtils.isEmpty(titleName)){
                return;
            }
            final Boolean[] flag = {false};
            List<Map<String, String>> list = SpringUtils.getBean(BoosTitleDeal.class).getBossTitleInfo(titleName.split("-")[0]);
            teamService.memberHandleThreadLocal(team, role -> {
                /**发称号*/
                if (list.size() <= 0) {
                    SpringUtils.getRoleService().addTitle(role, new TitleInfo(titleName.split("-")[1], titleName.split("-")[1]));
                    SpringUtils.getBean(BoosTitleDeal.class).addBoosTitle(role.getName(), titleName.split("-")[0], titleName.split("-")[1]);
                    sendRoleAchieve(role,bossNpc.getName());
                    flag[0] = true;
                }
                giveReward(role, fightEndEvent, bossNpc);
                sendRoleImg(role,bossNpc.getName());
            });
            // 更新数据库首次称号奖励
            if (flag[0]) {
                sendShouShaReward(team,bossNpc,titleName.split("-")[1]);
                SpringUtils.getBean(BoosTitleDeal.class).updateBoosTitle();
            }
        }
    }
    private void sendRoleImg(Role role,String bossName){
        if("昊天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.HAOTIANJUN);
        }else if("阳天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.YANGTIANJUN);
        }else if("赤天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.CHITIANJUN);
        }else if("朱天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.ZHUTIANJUN);
        }else if("成天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.CHENGTIANJUN);
        }else if("幽天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.YOUTIANJUN);
        }else if("玄天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.XUANTIANJUN);
        }else if("变天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.BIANTIANJUN);
        }else if("钧天君".equals(bossName)){
            addRoleBossImg(role, BossImgType.JUNTIANJUN);
        }
    }
    private void sendRoleAchieve(Role role,String bossName){
        if("昊天君".equals(bossName)){

        }else if("阳天君".equals(bossName)){

        }else if("赤天君".equals(bossName)){

        }else if("朱天君".equals(bossName)){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"602005");
        }else if("成天君".equals(bossName)){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"602027");
        }else if("幽天君".equals(bossName)){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"602035");
        }else if("玄天君".equals(bossName)){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"602014");
        }else if("变天君".equals(bossName)){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"602003");
        }else if("钧天君".equals(bossName)){
            SpringUtils.getBean(RoleService.class).addAchieveScore(role,"602021");
        }
    }

    private String getTitleName(String bossName){
        String titleName = "";
        // private static final List<String> SUPER_BOSS_LIST = Arrays.asList("黑熊妖皇", "血炼魔猪", "魅影蝎后", "赤血鬼猿", "紫衣妖凰");
        switch (bossName){
            case "昊天君":
                titleName = "jiutian_hao-九天真君·昊天君";
                break;
            case "阳天君":
                titleName = "jiutian_yang-九天真君·阳天君";
                break;
            case "赤天君":
                titleName = "jiutian_chi-九天真君·赤天君";
                break;
            case "朱天君":
                titleName = "jiutian_zhu-九天真君·朱天君";
                break;
            case "成天君":
                titleName = "jiutian_cheng-九天真君·成天君";
                break;
            case "幽天君":
                titleName = "jiutian_you-九天真君·幽天君";
                break;
            case "玄天君":
                titleName = "jiutian_xuan-九天真君·玄天君";
                break;
            case "变天君":
                titleName = "jiutian_bian-九天真君·变天君";
                break;
            case "钧天君":
                titleName = "jiutian_jun-九天真君·钧天君";
                break;
        }
        return titleName;
    }

}
